# -*- coding: utf-8 -*-
import random

def display(v,score): #显示棋盘
    print('{0:4}{1:4}{2:4}{3:4}'.format(v[0][0],v[0][1],v[0][2],v[0][3]))
    print('{0:4}{1:4}{2:4}{3:4}'.format(v[1][0],v[1][1],v[1][2],v[1][3]))
    print('{0:4}{1:4}{2:4}{3:4}'.format(v[2][0],v[2][1],v[2][2],v[2][3]))
    print('{0:4}{1:4}{2:4}{3:4}'.format(v[3][0],v[3][1],v[3][2],v[3][3]))
    print('得分为:%d' % (score))

def init(v): #初始化棋盘
    for i in range(4):
        for j in range(4):
            v[i][j] = random.choice([0,0,0,0,2,2,4])

   
def yidong(v,direction): #将棋盘向某个方向移动 
    if direction == 'Left':
        for i in range(4):
            num = v[i].count(0) #统计一行中0的数量
            for times in range(num):
                v[i].remove(0)
            for times in range(num):
                v[i].append(0)
                
    elif direction == 'Right':
        for i in range(4):
            num = v[i].count(0)
            for times in range(num):
                v[i].remove(0)
            for times in range(num):
                v[i].insert(0,0)

#将棋盘向某个方向合并
def merge(v,direction):
    score = 0
    if direction == 'Left':
        for i in range(4):
            for j in range(3):
                if v[i][j] == v[i][j+1] != 0:
                    v[i][j] *= 2
                    v[i][j+1] = 0
                    score += v[i][j]
    elif direction == 'Right':
        for i in range(4):
            for j in range(3,0,-1):
                if v[i][j] == v[i][j-1] != 0:
                    v[i][j] *= 2
                    v[i][j-1] = 0
                    score += v[i][j]
    return score

def insert(v): #随机添加数
    num0 = 0 #记录0的数量
    b = 0
    for i in range(4):
        num0 += v[i].count(0)
    a = random.randrange(num0)
    num = random.choice([2,2,2,4])
    for j in range(4):
        for k in range(4):
            if v[j][k] == 0:
                if b == a:
                    v[j][k] = num
                b +=1                            

def can_move(v):
    for i in range(1,3):
        for j in range(4):
            if v[i][j] == v[i-1][j] or v[i][j] == v[i+1][j]:
                return True
    for k in range(4):
        for l in range(1,3):
            if v[k][l] == v[k][l-1] or v[k][l] == v[k][l+1]:
                return True
    return False
    
def transpose(M):
    result = []
    for i in range(4):
        item = []
        for index in range(4):
            item.append(M[index][i])
        result.append(item)
    return result
            
def handle(v):
    score = 0
    move = input('W is up, S is down, A is left, D is right.\n请输入：')
    if move in ['A', 'a']:
        yidong(v,'Left')
        score += merge(v,'Left')
        yidong(v,'Left')
        score = merge(v,'Left')
        
    elif move in ['D', 'd']:
        yidong(v,'Right')
        score += merge(v,'Right')
        yidong(v,'Right')
        score = merge(v,'Right')
        
    elif move in ['W', 'w']:
        v1 = transpose(v)
        yidong(v1,'Left')
        score += merge(v1,'Left')
        yidong(v1,'Left')
        score += merge(v1,'Left') 
        v = transpose(v1)       
        
    elif move in ['S', 's']:
        v1 = transpose(v)
        yidong(v1,'Right')
        score += merge(v1,'Right')
        yidong(v1,'Right')
        score += merge(v1,'Right') 
        v = transpose(v1)
    else:
        print('input is wrong,please try again')
    return score,v
    
    
def main():
    v = [[0,0,0,0],
     [0,0,0,0],
     [0,0,0,0],
     [0,0,0,0]]
    score = 0
    init(v)
    display(v,score)
    while True:
        num0 = 0
        for i in range(4):
            num0 += list(v[i]).count(0)
        if score > 1024:
            print('You Win!')
            break
        elif can_move(v) == False:
            print('You lose')
            break
        else:
            score_add,v = handle(v)
            score += score_add
            insert(v)
            display(v,score)

if __name__ == '__main__':
    main()